import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import {
    InitializeWorkingHourServiceProxy,
    CreateOrUpdateInitWorkingHourInput,
    InitWorkingHourEditDto
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { CalendarModule } from 'primeng/calendar';
import { SelectItem, LazyLoadEvent } from 'primeng/api';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';

@Component({
    selector: 'createOrEditInitWorkingHourModal',
    templateUrl: './create-or-edit-init-working-hour-modal.component.html'
})
export class CreateOrEditInitWorkingHourModalComponent extends AppComponentBase implements OnInit {
    constructor(injector: Injector, private _initializeWorkingHourServiceProxy: InitializeWorkingHourServiceProxy) {
        super(injector);
    }
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    editDto = new InitWorkingHourEditDto();
    filterText = '';
    deviceName = '';
    deviceNumber = '';
    sorting = '';
    maxResultCount?: number;
    SkipCount?: number;
    deviceId?: number;
    deviceNumberList: SelectItem[];
    // deviceList = new Array<CostOfDeviceListDto>();
    ngOnInit(): void {}
    show(id?: number): void {
        this.active = true;

        this._initializeWorkingHourServiceProxy.getForEdit(id).subscribe(result => {
            this.editDto = result.initWorkingHour;
            const self = this;
            this.modal.show();
        });
    }

    save(): void {
        this.saving = true;
        let input = new CreateOrUpdateInitWorkingHourInput();
        input.initWorkingHour = this.editDto;

        this._initializeWorkingHourServiceProxy
            .createOrUpdate(input)
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
